package com.profgj.profgj.controller;

import com.profgj.profgj.enums.ServerStatus;
import com.profgj.profgj.utils.ReturnOperator;
import com.profgj.profgj.vo.ResultMsg;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@RestController
@RequestMapping("/usrOp")
@Api(tags = {"用户操作日志"}, description = "用户操作日志")
public class UserOpController {

    /**
     * 用户操作日志文件路径
     */
    @Value("${system.userOperation}")
    private String desPath = null;

    /**
     * 用户操作日志文件名
     */
    @Value("${system.userOpfilename}")
    private String fileName = null;

    /**
     * 查询用户操作日志
     * @param date
     * @return
     */
    @GetMapping("/log/{date}")
    @ApiOperation(value = "查询操作日志",produces = "application/json",consumes = "application/json")
    @ApiImplicitParam(name = "date", value = "查询的日期",required = true, dataType = "Date", paramType = "Date")
    @ApiResponses({
            @ApiResponse(code = 200, message = "表明该请求被成功地完成,所请求的资源发送到客户端"),
            @ApiResponse(code = 401, message = "请求要求身份验证,常见于需要登录而用户未登录的情况"),
            @ApiResponse(code = 403, message = "服务器拒绝请求,常见于机密信息或复制其它登录而用户未登录的情况"),
            @ApiResponse(code = 404, message = "服务器无法取得所请求的网页,请求资源不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public ResponseEntity<ResultMsg> queryUserOpLog(@PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date) {

        // 创建日期时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 将日期转字符串
        String strDate = sdf.format(date);
        // 拼接文件完整路径
        File userOpFile = new File(desPath + strDate + "/" + fileName);
        // 判断文件是否存在
        if (!userOpFile.exists()) {
            // 不存在返回数据
            return ReturnOperator.createResultMsg("", "该日期没有用户记录日志", ServerStatus.ERROR,
                    new ArrayList(), new Boolean(false), HttpStatus.OK);
        }

        BufferedReader reader = null;
        // 字符串缓冲区
        StringBuffer strBuffer = new StringBuffer();
        String tempString = null;
        try {
            // 创建文本读缓冲区对象
            reader = new BufferedReader(new FileReader(userOpFile));
            // 循环行读取
            while ((tempString = reader.readLine()) != null) {
                // 追加到字符串换乘区 并拼接 换行和回车符
                strBuffer.append(tempString + "\r\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // 文件没有找到 异常 返回数据
            return ReturnOperator.createResultMsg("", "没有找到该日志文件", ServerStatus.ERROR,
                    new ArrayList(), new Boolean(false), HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            // 文件读取异常 返回数据
            return ReturnOperator.createResultMsg("", "文件读取异常", ServerStatus.ERROR,
                    new ArrayList(), new Boolean(false), HttpStatus.OK);
        }finally {
            try {
                // 读取完文件将其关闭
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 返回数据
        return ReturnOperator.createResultMsg("日志文件读取成功","",ServerStatus.OK,
                new ArrayList(),strBuffer.toString(),HttpStatus.OK);
    }
}
